/// 首页
/// 
/// 应用的主页面
/// 展示欢迎信息和功能示例
/// 
/// 作者: Fluttert Team
/// 日期: 2024

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';
import '../../providers/app_provider.dart';
import '../../app/routes.dart';
import '../../services/native_service.dart';
import '../../widgets/common/loading_widget.dart';

/// 首页组件
/// 
/// 使用Consumer监听AppProvider状态变化
/// 根据加载状态显示不同的UI
class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('首页'),
      ),
      body: Consumer<AppProvider>(
        builder: (context, provider, child) {
          // 根据加载状态显示加载指示器或内容
          if (provider.isLoading) {
            return const LoadingWidget();
          }

          return ListView(
            padding: const EdgeInsets.all(16),
            children: [
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '欢迎使用 Fluttert',
                        style: Theme.of(context).textTheme.headlineSmall,
                      ),
                      const SizedBox(height: 8),
                      Text(
                        '这是一个完整的 Flutter 项目框架',
                        style: Theme.of(context).textTheme.bodyMedium,
                      ),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: () {
                  provider.setLoading(true);
                  Future.delayed(const Duration(seconds: 2), () {
                    provider.setLoading(false);
                  });
                },
                child: const Text('测试加载状态'),
              ),
              const SizedBox(height: 16),
              ElevatedButton.icon(
                onPressed: () {
                  context.push(AppRoutes.userList);
                },
                icon: const Icon(Icons.people),
                label: const Text('用户列表'),
              ),
              const SizedBox(height: 16),
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '原生平台交互示例',
                        style: Theme.of(context).textTheme.titleMedium,
                      ),
                      const SizedBox(height: 8),
                      ElevatedButton(
                        onPressed: () async {
                          final deviceInfo = await NativeService.getDeviceInfo();
                          if (deviceInfo != null && context.mounted) {
                            showDialog(
                              context: context,
                              builder: (ctx) => AlertDialog(
                                title: const Text('设备信息'),
                                content: Column(
                                  mainAxisSize: MainAxisSize.min,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text('平台: ${deviceInfo['platform']}'),
                                    Text('版本: ${deviceInfo['version']}'),
                                    Text('型号: ${deviceInfo['model'] ?? '未知'}'),
                                  ],
                                ),
                                actions: [
                                  TextButton(
                                    onPressed: () => Navigator.pop(ctx),
                                    child: const Text('确定'),
                                  ),
                                ],
                              ),
                            );
                          }
                        },
                        child: const Text('获取设备信息'),
                      ),
                      const SizedBox(height: 8),
                      ElevatedButton(
                        onPressed: () async {
                          await NativeService.showToast('这是原生Toast消息');
                        },
                        child: const Text('显示Toast'),
                      ),
                      const SizedBox(height: 8),
                      ElevatedButton(
                        onPressed: () async {
                          final level = await NativeService.getBatteryLevel();
                          if (level != null && context.mounted) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(content: Text('电池电量: $level%')),
                            );
                          }
                        },
                        child: const Text('获取电池电量'),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}

